<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

/**
 * Description of catalogue
 *
 * @author b4by
 */
class catalogue extends MY_Controller {

	function __construct() {
		parent::__construct();
		$this->load->model('model_catalogue');
		$this->load->model('model_catalogue_info');
	}

	public function index() {
		$this->data['temp'] = 'admin/catalogue/index';
		$this->load->view('admin/main', $this->data);
	}

	public function read() {
		$catalogues = $this->model_catalogue->get_list_hash($this->siteConfig->lang, array(
			'order' => array('index', 'asc')
		));

		array_to_object($catalogues);

		object_prefix_create($catalogues, '', '&nbsp;&nbsp;&nbsp;&nbsp;');

		$data = array();
		object_to_array($data, $catalogues);

		echo json_encode(array(
			'state' => 'success',
			'data' => $data,
		));
	}

	public function create() {
		$this->load->helper('form');
		$this->load->library('form_validation');

		$this->form_validation->set_rules('index', 'lang:catalogue_index', 'is_natural');
		$this->form_validation->set_rules('info', 'lang:catalogue_name', 'required|callback__check_array');
		$this->form_validation->set_rules('p_id', 'lang:catalogue_parent', 'callback__check_id');
		$this->form_validation->set_rules('type', 'lang:catalogue_type', 'callback__check_type');

		if ($this->form_is_valid()) {
			$data = request_filter(array('p_id', 'index', 'image', 'css', 'type', 'home', 'active', 'link', 'menu', 'main'));
			if ($data['main'] == 1)
				$this->model_catalogue->update_rule(array(), array(
					'main' => 0,
				));
			$cat_id = $this->model_catalogue->create($data);
			$info = request('info');
			foreach ($this->languages as $lang) {
				$info[$lang->id]['cat_id'] = $cat_id;
				$info[$lang->id]['lang_id'] = $lang->id;
			}
			foreach ($info as $row) {
				$this->model_catalogue_info->create($row);
			}
			echo json_encode(array(
				'state' => 'success',
				'title' => lang('success'),
				'message' => lang('catalogue_create_success'),
			));
		} else {
			echo json_encode(array(
				'state' => 'error',
				'title' => lang('error'),
				'errors' => $this->data['errors']
			));
		}
	}

	public function _check_type($val) {
		if ($val < 3)
			return TRUE;
		$this->form_validation->set_message(__FUNCTION__, lang('catalogue_invalid_type'));
		return FALSE;
	}

	public function update() {
		$this->load->helper('form');
		$this->load->library('form_validation');

		$this->form_validation->set_rules('index', 'lang:catalogue_index', 'is_natural');
		$this->form_validation->set_rules('info', 'lang:catalogue_name', 'callback__check_array');
		$this->form_validation->set_rules('p_id', 'lang:catalogue_parent', 'callback__check_pid');

		if ($this->form_validation->run()) {
			$id = request('id');
			$data = request_filter(array('p_id', 'index', 'image', 'css', 'active', 'type', 'home', 'link', 'menu', 'main'));
			if ($data['main'] == 1)
				$this->model_catalogue->update_rule(array(), array(
					'main' => 0,
				));
			$cat_id = $this->model_catalogue->update($id, $data);
			$info = request('info');
			foreach ($this->languages as $lang) {
				$info[$lang->id]['cat_id'] = $cat_id;
				$info[$lang->id]['lang_id'] = $lang->id;
			}
			foreach ($info as $row) {
				$data = params_filter($row, array('name', 'description'));
				$this->model_catalogue_info->update_rule(array(
					'cat_id' => $id,
					'lang_id' => $row['lang_id'],
						), $data);
			}
			echo json_encode(array(
				'state' => 'success',
				'title' => lang('notice'),
				'message' => lang('catalogue_update_success'),
			));
		} else {
			echo json_encode(array(
				'state' => 'error',
				'title' => lang('error'),
				'errors' => $this->data['errors']
			));
		}
	}

	public function delete() {
		$this->load->helper('form');
		$this->load->library('form_validation');

		$this->form_validation->set_rules('id', 'lang:catalogue', 'required,callback__check_delete_id');
		if ($this->form_validation->run()) {
			$id = request('id');
			$this->model_catalogue->del($id);
			$this->model_catalogue_info->del_rule(array(
				'cat_id' => $id
			));
			echo json_encode(array(
				'state' => 'success',
				'id' => $id,
					//'title'	=> lang('notice'),
					//'message'	=> lang('catalogue_delete_success')
			));
		} else {
			echo json_encode(array(
				'state' => 'error',
				'title' => lang('error'),
				'message' => form_error_message(array('id'))
			));
		}
		$id = request('id');
	}

	public function detail() {
		$id = request('id');

		$catalogue = $this->model_catalogue->get_info($id);
		if ($catalogue !== false) {
			$catalogues = $this->model_catalogue->get_list_hash($this->siteConfig->lang, array('order' => array('index', 'asc')));

			array_to_object($catalogues);
			//pr($catalogue);die();
			delete_children($catalogue, $catalogues);
			object_prefix_create($catalogues, '', '&nbsp;&nbsp;&nbsp;&nbsp;');
			$catalogue->parents = array();
			object_to_array($catalogue->parents, $catalogues);
			echo json_encode(array(
				'state' => 'success',
				'data' => $catalogue,
			));
		} else {
			echo json_encode(array(
				'state' => 'error',
				'title' => lang('error'),
				'message' => lang('catalogue_not_exist'),
			));
		}
	}

	public function _check_id($id) {
		if ($id != 0 && $this->model_catalogue->get_info($id) === false) {
			$this->form_validation->set_message(__FUNCTION__, lang('catalogue_not_exist'));
			return false;
		}
		return true;
	}

	public function _check_delete_id($id) {
		if (!$this->_check_id($id))
			return false;
		$catalogues = $this->model_catalogue->get_list($this->siteConfig->lang, array(
			'where' => array('p_id' => $id)
		));
		if (count($catalogues) > 0) {
			$this->form_validation->set_message(__FUNCTION__, lang('catalogue_contain_child'));
			return false;
		}
		return true;
	}

	public function _check_array($array) {
		foreach ($this->languages as $lang) {

			if (!isset($array[$lang->id]) ||
					!isset($array[$lang->id]['name']) ||
					strlen($array[$lang->id]['name']) == 0) {
				$this->form_validation->set_message(__FUNCTION__, lang('catalogue_name_required'));
				return false;
			}
		}
		return true;
	}

	public function _check_pid($p_id) {
		if ($p_id == 0)
			return true;
		$id = request('id');
		$catalogue = $this->model_catalogue->get_info($id);
		$catalogues = $this->model_catalogue->get_list_hash($this->siteConfig->lang);
		//pr($catalogue);die();
		array_to_object($catalogues);
		delete_children($catalogue, $catalogues);
		$data = array();
		object_to_array($data, $catalogues);

		foreach ($data as $row) {
			if ($p_id == $row->id)
				return true;
		}
		$this->form_validation->set_message(__FUNCTION__, lang('catalogue_invalid_parent'));
		return false;
	}

}

?>
